package com.lx.fav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lx.fav.entity.User;
import com.lx.fav.dao.UserMapper;
import com.lx.fav.service.BizException;
import com.lx.fav.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lx.fav.util.MBHelp;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lx
 * @since 2024-10-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    public User login(String name, String pwd) throws BizException{


        if( name == null || name.isBlank()){
            throw new BizException("请输入用户名!", -2);
        }
        if( pwd == null || pwd.isBlank()){
            throw new BizException("请输入密码!", -3);
        }
        if( pwd.length() < 3){
            throw new BizException("密码不能小于三位!", -4);
        }

        User user = MBHelp.execute(session ->{
            UserMapper mapper = session.getMapper(UserMapper.class);
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getName, name);
            lqw.eq(User::getPwd, pwd);
            return mapper.selectOne(lqw);
        });
        if( user == null){
            throw new BizException("用户名或者密码错误!", -1);
        }
        return user;
    }

}
